//
//  testNewGongNengViewController.m
//  EducationAdminClient
//
//  Created by KWSD_F on 16/4/5.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "testNewGongNengViewController.h"
#import "AddCell.h"
#import "leftChoiceViewController.h"
#import "rightChoiceViewController.h"
@interface testNewGongNengViewController ()<NextDelegate>

{
    UITableView *_myTableView;
    NSMutableArray *_dataArr;
    NSMutableArray *_cellArr;
}

@end

@implementation testNewGongNengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    // Do any additional setup after loading the view.
    _dataArr = [NSMutableArray array];
    _cellArr = [NSMutableArray array];
    [_dataArr addObject:@"数据1"];
    [_dataArr addObject:@"添加"];
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    _myTableView.editing = YES;
//    [_myTableView setEditing:YES animated:YES];
    [_myTableView setAllowsSelectionDuringEditing:YES];
    
}

#pragma -mark testBtnClick
- (void)btnClick
{
    NSLog(@"aaabbbccc");
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_dataArr count] inSection:0];
    
    NSString *str = [NSString stringWithFormat:@"数据%zd",indexPath.row+1];

    [_dataArr  insertObject:str atIndex:indexPath.row];
    //然后在对应的位置添加一个cell（会自动匹配我们刚刚插入的数据）
    [_myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

#pragma -mark TableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellId";
    
    AddCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[AddCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
    }
//    [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
    cell.leftBtn.indexPath = indexPath;
    //.....
    
    //  indexPath代表cell的位置
    //  indexPath.section代表在第几段
    //  indexPath.row代表在第几行
    
    NSLog(@"section = %zd,row = %zd",indexPath.section,indexPath.row);
    
    //通过cell的位置找到对应的保存数据的对象（数据模型）
    
    //将对应的信息展示在cell自带的2个label上
    if (indexPath.row<_dataArr.count-1) {
//        cell.textLabel.text = _dataArr[indexPath.row];
        if (cell.leftBtn.hidden) {
            cell.leftBtn.hidden = NO;
            cell.rightBtn.hidden = NO;
            cell.textLabel.text = @"";
        }
        [cell.leftBtn setTitle:_dataArr[indexPath.row] forState:UIControlStateNormal];
        [cell.rightBtn setTitle:@"暂无" forState:UIControlStateNormal];
        [cell createBtnClickAddTarget:self action:@selector(testBtnClick:) forControlEvents:UIControlEventTouchUpInside button:cell.leftBtn];
        [cell createBtnClickAddTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside button:cell.rightBtn];
        NSLog(@"cell%@",cell.leftBtn.titleLabel.text);
//#pragma -mark 设置cell的tag值为400+indexPath.row
//        cell.tag = indexPath.row+400;
//        cell.leftBtn.tag = cell.tag+400;
//        cell.rightBtn.tag = cell.tag+500;
    }else{
        cell.textLabel.text = _dataArr[indexPath.row];
        cell.leftBtn.hidden = YES;
        cell.rightBtn.hidden = YES;
        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//        button.frame = cell.frame;
//        [button setTitle:_dataArr[indexPath.row] forState:UIControlStateNormal];
//        [cell.contentView addSubview:button];
//        [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
    
    return cell;

}


-(void)sendText:(NSString *)text andPath:(NSInteger)path
{
    
    NSLog(@"￥return path :%zd￥",path);
//    for (NSString *str in _dataArr) {
//        NSLog(@"原来的数组%@",str);
//    }
    NSLog(@"数组是这个%@",_dataArr[path]);
    _dataArr[path] = text;
//    for (NSString *str in _dataArr) {
//        NSLog(@"新数组%@",str);
//    }
    [_myTableView reloadData];
//    for (NSInteger i = 0; i<_dataArr.count-1; i++) {
//        if ([_dataArr[i] isEqualToString:text]) {
//            NSLog(@"-------duilou@J#$K@Q#J$KJ@#QR---%zd---",i);
//            NSLog(@"%@",_dataArr[i]);
//            NSLog(@"%@",_dataArr[path]);
//        }
//    }
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //允许编辑,编辑相关按钮（删除，插入）点击时触发
    NSLog(@"aaa");
    
    //判断编辑风格
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        if ([_cellArr count]!=0) {
            [_cellArr removeAllObjects];
        }
        [_cellArr addObject:[tableView visibleCells]];
        NSLog(@"%@",_cellArr);
        NSArray *arr = _cellArr[0];
        NSLog(@"%zd",indexPath.row);
        AddCell *cell = arr[indexPath.row];
        NSLog(@"%@",cell.leftBtn.titleLabel.text);
        NSLog(@"%@",arr);
        
        
        //通过位置先删除对应的数据模型
        //从大数组里找到小数组，再从小数组里面删掉对应的数据
        [_myTableView beginUpdates];
        [_dataArr  removeObjectAtIndex:indexPath.row];
        for (NSInteger i = indexPath.row; i<arr.count-1; i++) {
            AddCell *cell = arr[i];
            NSIndexPath *path = [NSIndexPath indexPathForRow:cell.leftBtn.indexPath.row-1 inSection:indexPath.section];
            cell.leftBtn.indexPath = path;
            
        }
        //然后再删除对应的cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [_myTableView endUpdates];
//        [_myTableView reloadData];
        NSLog(@"%@",_cellArr);
        
        
        
        
        NSLog(@"-----%@======",_dataArr);

//        [_myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    }if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSString *str = [NSString stringWithFormat:@"数据%zd",indexPath.row+1];
        //在数据源对应的位置插入新数据
        [_dataArr  insertObject:str atIndex:indexPath.row];
        //然后在对应的位置添加一个cell（会自动匹配我们刚刚插入的数据）
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置cell的编辑风格（插入/删除）
    if (indexPath.row <[_dataArr  count]-1) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleInsert;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row);
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == _dataArr.count-1) {
        NSString *str = [NSString stringWithFormat:@"数据%zd",indexPath.row+1];
        //在数据源对应的位置插入新数据
        NSLog(@"之前的：%@",_dataArr);
        [_dataArr  insertObject:str atIndex:indexPath.row];
        NSLog(@"之后的：%@",_dataArr);
//        NSIndexPath *indexMy = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
        //然后在对应的位置添加一个cell（会自动匹配我们刚刚插入的数据）
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    
    
    
    
}


#pragma -mark rightBtnClick
- (void)rightBtnClick:(UIButton *)sender{
    NSLog(@"rightSB");
}


#pragma -mark leftBtnClick
- (void)testBtnClick:(NormalButton *)sender{
//    AddCell *cell = (AddCell *)[_myTableView viewWithTag:sender.tag-400];
//    NSInteger testPath = 0;
    
//    NSLog(@"￥sendertag = %zd￥",sender.tag);
//    for (int i = 0; i<_dataArr.count-1; i++) {
//        if ([sender.titleLabel.text isEqualToString:_dataArr[i]]) {
//            testPath = i;
//        }
//    }
//    NSLog(@"%@",cell.textLabel.text);
//    NSLog(@"leftSB%@",sender.titleLabel.text);
    leftChoiceViewController *lcvc = [[leftChoiceViewController alloc] init];
    lcvc.path = sender.indexPath.row;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:lcvc];
    lcvc.delegate = self;
    [self presentViewController:nc animated:YES completion:^{
        NSLog(@"-----aaaa+++++++");
    }];
    
    NSLog(@"%zd",sender.indexPath.row);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
